package com.siloam.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.siloam.dao.ArticleDao;
import com.siloam.module.Article;
import com.siloam.vo.ArticleVO;
import com.siloam.vo.PageVO;

public class ArticleService {
	public ArticleDao articleDao;

	public ArticleDao getArticleDao() {
		return articleDao;
	}

	public void setArticleDao(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}
	
	@Transactional
	public void add(Article article){
		articleDao.add(article);
	}
	
	public PageVO<ArticleVO> findUserArticles(int userId, int rowBeginIndex, int rowCount){
		List<Article> articles = articleDao.findUserArticle(userId, rowBeginIndex, rowCount);
		List<ArticleVO> articleVOs = new ArrayList<ArticleVO>();
		for(Article article : articles){
			articleVOs.add(new ArticleVO(article));
		}
		PageVO<ArticleVO> pageVO = new PageVO<ArticleVO>();
		pageVO.setRows(articleVOs);
		pageVO.setTotal(articleDao.userArticleCount(userId));
		return pageVO;
	}
	
	public ArticleVO load(int id){
		return new ArticleVO(articleDao.load(id));
	}
	
	public Article loadArticle(int id){
		return articleDao.load(id);
	}
	
	public void delete(int id){
		articleDao.delete(id);
	}
	
	public void update(Article article){
		articleDao.update(article);
	}
	
}
